package com.hmall.controller.globalexception;

import com.hdmall.business.common.common.ServerResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import javax.servlet.http.HttpServletRequest;

/**
 * 处理token失效
 * 全局异常处理
 */
@ControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
    /**
     * 在controller里面内容执行之前，校验一些参数不匹配啊，Get post方法不对啊之类的
     */
  /*  @Override
    protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers, HttpStatus status, WebRequest request) {
        System.out.println("错误");

        return new ResponseEntity<Object>("出错了", NOT_EXTENDED);

    }*/
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ServerResponse jsonHandler(HttpServletRequest request, Exception e) throws Exception {
      /*  ErrorInfo<String> r = new ErrorInfo<>();
        r.setMessage(e.getMessage());
        r.setCode(ErrorInfo.ERROR);
        r.setData("发生异常");
        r.setUrl(request.getRequestURL().toString());*/
        if (e.getMessage().equals("无该用户,请重新登录")){
            return  ServerResponse.createByStatus(11,e.getMessage(),"");
        }
        if (e.getMessage().equals("用户已失效,请重新登录")){
            return  ServerResponse.createByStatus(12,e.getMessage(),"");
        }
        if (e.getMessage().equals("找不到配送方式")){
            return  ServerResponse.createByStatus(51,e.getMessage(),"");
        }
        return  ServerResponse.createByStatus(200,e.getMessage(),"");
    }
}
